﻿//-----------------------------------------------------------------------
// <copyright file="Place.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Knihotoc_WP8.Data.Storage;
using Knihotoc_WP8.Localization;

namespace Knihotoc_WP8.Data
{
    public class Place : IIsolatedStorage<Place>
    {
        #region Constructors
        public Place()
        {
        }

        public Place(int id, string name, float lat, float lon, string logoImagePath)
        {
            this.ID = id;

            if (name != null && name.Length > 0)
            {
                this.Name = name;
            }

            this.Lat = lat;
            this.Lon = lon;
            this.LogoImagePath = logoImagePath;

            this.SaveToIsolatedStorage();
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the ID of the place.
        /// </summary>
        public int ID { get; set; }
        
        /// <summary>
        /// Gets or sets the name of the place.
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// Gets or sets the verbal description of the place.
        /// </summary>
        public string Description { get; set; }
        
        /// <summary>
        /// Gets or sets the geography position latitude.
        /// </summary>
        public float Lat { get; set; }
        
        /// <summary>
        /// Gets or sets the geography position longitude.
        /// </summary>
        public float Lon { get; set; }
        
        /// <summary>
        /// Gets or sets the address of the place.
        /// </summary>
        public string Address { get; set; }
        
        /// <summary>
        /// Gets or sets the path to the logo of the place.
        /// </summary>
        public string LogoImagePath { get; set; }

        public double Distance { get; set; }
        public string DistanceString { get; set; }
        #endregion

        public bool SaveToIsolatedStorage()
        {
            bool everythingAdded = true;

            if (this.Name != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Name", this.Name))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                return false;
            }

            if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Lat", this.Lat.ToString()))
            {
                everythingAdded = false;
            }

            if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Lon", this.Lon.ToString()))
            {
                everythingAdded = false;
            }

            if (everythingAdded)
            {
                IsolatedStorageHandler.Instance.Save();
                return true;
            }
            else
            {
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Name");
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Lat");
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Lon");

                return false;
            }
        }

        public void LoadFromIsolatedStorage(string id)
        {
            this.Name = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Place_" + id + "_Name", LocalizedStrings.Get("NameNotFound"));

            this.Address = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Place_" + id + "_Address", LocalizedStrings.Get("AddressNotFound"));

            this.Lat = Convert.ToSingle(IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Place_" + id + "_Lat", LocalizedStrings.Get("LatNotFound")));

            this.Lon = Convert.ToSingle(IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Place_" + id + "_Lon", LocalizedStrings.Get("LonNotFound")));
        }
    }
}
